﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Practices.Prism.Mvvm;
using AesonPrismMonoProject.Tools;

namespace AesonPrismMonoProject.LocalConsole
{
    /// <summary>
    /// Interaction logic for AesonClientConsole.xaml
    /// </summary>
    public partial class AesonClientConsole : IView
    {
        TextWriter _writer = null;
        ConsoleColor c;
        public AesonClientConsole()
        {
            InitializeComponent();
            // Instantiate the writer
            _writer = new TextBoxStreamWriter(TxbConsole);
            // Redirect the out Console stream
            Console.SetOut(_writer);

            Console.WriteLine("Now redirecting output to the text box");
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
            this.Visibility = System.Windows.Visibility.Hidden;
        }

        public void ShowConsole()
        {
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            this.Show();

            TxbConsole.ScrollToEnd();
            TxbCommand.Focus();
        }

        private void AesonClientConsole_OnLoaded(object sender, RoutedEventArgs e)
        {
            TxbConsole.ScrollToEnd();
            c = Console.ForegroundColor;
            TxbCommand.Focus();
        }

        private void TxbCommand_OnKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                var t = sender as TextBox;
                var s = t.Text;
                if (s == "exit")
                {
                    this.Close();
                    t.Text = string.Empty;
                }

            }
        }
    }
}
